package com.bit.inmap;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	EditText id, pwd;
	static boolean  login_check = false;
	BackPressCloseHandler closeHandler;
	static ImageButton loginBtn;
	
	//MySQLHandler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//handler = MySQLHandler.open(getApplicationContext());
		
		// �����ڵ鷯(Back ��ư �ι�Ŭ���� ����)
		closeHandler = new BackPressCloseHandler(this);

		loginBtn = (ImageButton) this.findViewById(R.id.imageView1);
		loginBtn.setOnClickListener(this);	//�α���
		if(login_check)	{
			loginBtn.setBackgroundResource(R.drawable.logout_c);
		}else{
			loginBtn.setBackgroundResource(R.drawable.login_c);
		}
		((ImageButton) this.findViewById(R.id.menu1)).setOnClickListener(this); // ��������
		((ImageButton) this.findViewById(R.id.menu2)).setOnClickListener(this); // �˸���
		((ImageButton) this.findViewById(R.id.menu3)).setOnClickListener(this); // �ð�ǥ
		((ImageButton) this.findViewById(R.id.menu4)).setOnClickListener(this); // �ַ���
		((ImageButton) this.findViewById(R.id.menu5)).setOnClickListener(this); // GPS
		
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.imageView1){
			if (!login_check) {
				Intent intent = new Intent(this, LoginActivity.class);
				startActivity(intent);
				
			} else if (login_check) {
				logout();
			}
		}
		else if(login_check){
		switch (v.getId()) {
				
			case R.id.menu1:
				Intent announce = new Intent(MainActivity.this, AnnounceActivity.class);
				startActivity(announce);
				break;
	
			case R.id.menu2:
				Intent qand = new Intent(MainActivity.this, QandActivity.class);
				startActivity(qand);
				break;
	
			case R.id.menu3:
				Intent timetable = new Intent(MainActivity.this,
						TimeTableActivity.class);
				startActivity(timetable);
				break;
	
			case R.id.menu4:
				Intent gallery = new Intent(MainActivity.this,
						AchieveActivity.class);
				startActivity(gallery);
				break;
			case R.id.menu5:
				Intent gps = new Intent(MainActivity.this,
						GPSActivity.class);
				startActivity(gps);
				break;

		}
		}else{
			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			//Toast.makeText(this, "�α����� ���ּ���", Toast.LENGTH_SHORT).show();
//			Toast toast = Toast.makeText(this, "�α����� ���ּ���", Toast.LENGTH_SHORT);
//			toast.setGravity(Gravity.CENTER, 0, 0);
//			toast.show();
		}
	}

	// �α׾ƿ� â ����
	private void logout() {
		Context context = this.getApplicationContext();
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.logout, null);

		AlertDialog.Builder aDialog = new AlertDialog.Builder(this);
		//aDialog.setTitle("�α׾ƿ� �Ͻðڽ��ϱ�");
		aDialog.setView(layout);

		aDialog.setPositiveButton("확인", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				login_check = false;
				logoutDisp();
			}

		});
		aDialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();

			}
		});
		AlertDialog ad = aDialog.create();
		ad.setView(layout,0,0,0,0);
		ad.show();
		
		ad.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));// ����ó��
		ad.setCanceledOnTouchOutside(false); // ���̾�α� �� ��ġ�� ��������ʱ�
		ad.setInverseBackgroundForced(true);
	}

	private void logoutDisp() {
		ImageButton iBtn = (ImageButton) this.findViewById(R.id.imageView1);
		iBtn.setBackgroundResource(R.drawable.login);

		Toast toast = Toast.makeText(this, "로그아웃 되었습니다", Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
		if(login_check)	{
			loginBtn.setBackgroundResource(R.drawable.logout_c);
		}else{
			loginBtn.setBackgroundResource(R.drawable.login_c);
		}
		
	}
	public void onBackPressed() {
		closeHandler.onBackPressed();
	}
	
}
